////////////////////////////////////////////////
// -- DepthHUD Lite                           //
// by Hurricaaane (Ha3)                       //
//                                            //
// http://www.youtube.com/user/Hurricaaane    //
//--------------------------------------------//
// Base panel                                 //
////////////////////////////////////////////////

local PANEL = {}

surface.CreateFont("DIN Light" , 36, 400 , true, true, "DHLITE_NormalText" )
surface.CreateFont("DIN Light" , 28, 400 , true, true, "DHLITE_Text" )
surface.CreateFont("DIN Light", 22, 400 , true, true, "DHLITE_RegularText" )
surface.CreateFont("DIN Light", 14, 400 , true, true, "DHLITE_SmallText" )
surface.CreateFont("halflife2" , 28, 2   , true, true, "DHLITE_Number" )
surface.CreateFont("halflife2" , 24, 0   , true, true, "DHLITE_RegularNumber" )
surface.CreateFont("halflife2" , 20, 0   , true, true, "DHLITE_SmallNumber" )
surface.CreateFont("Alterebro Pixel Font Regular", 12, 0, false, true, "DHLITE_RegularPixel" )
surface.CreateFont("Alterebro Pixel Font Regular", 10, 0, false, true, "DHLITE_SmallPixel" )
surface.CreateFont("Trebuchet MS", 16, 0 , false, false, "DHLITE_Instruction" )

function PANEL:Init()
	self:SetSkin( dhlite._skin_name )
	self:SetCanvas( self:GetParent() or nil )
	
	self._iSnapRes = 32
	self._iSnapX = 0
	self._iSnapY = 0
	
	self._bStored = false
	
	self._show = false
	
	self._duration = 0.5
	self._showAt = -1000
	self._hideAt = -900
	
end

function PANEL:IsHidden( )
	return not self._show
end

function PANEL:GetVisibility( )
	local fVis = self._show and (1 - (self._showAt - CurTime()) / self._duration)
			or (self._hideAt - CurTime()) / self._duration
	return (fVis > 1) and 1 or (fVis < 0) and 0 or fVis
	
end

function PANEL:Show( panel )
	if self._show then return end
	self._show = true
	
	self._showAt = CurTime() + self._duration
	if (self._hideAt > CurTime()) then
		self._showAt = self._showAt - self._hideAt - CurTime()
		
	end
	
end

function PANEL:Hide( panel )
	if not self._show then return end
	self._show = false
	
	self._hideAt = CurTime() + self._duration
	if (self._showAt > CurTime()) then
		self._hideAt = self._hideAt - self._showAt - CurTime()
		
	end
	
	
end

function PANEL:SetCanvas( panel )
	self._canvas = panel
	
end

function PANEL:GetCanvas( )
	return self._canvas
	
end

function PANEL:OnMousePressed( mousecode )
	dhlite:RequestMouseInput( self, mousecode )
	if self.DoMousePressed then
		self.DoMousePressed( )
		
	end
	
end

function PANEL:SetVirtualGridPos( iSnapRes, iSnapX, iSnapY )
	self._iSnapX = iSnapX or ((self._iSnapX / self._iSnapRes) * iSnapRes)
	self._iSnapY = iSnapY or ((self._iSnapY / self._iSnapRes) * iSnapRes)
	self._iSnapRes = iSnapRes
	
	self._iSnapX = (self._iSnapX > self._iSnapRes) and self._iSnapRes
					or (self._iSnapX < 0) and 0
					or self._iSnapX
	self._iSnapY = (self._iSnapY > self._iSnapRes) and self._iSnapRes
					or (self._iSnapY < 0) and 0
					or self._iSnapY
	
end

function PANEL:EvaluateVirtualGridPos( snapOverride, offsx, offsy )
	local canvas = self:GetCanvas()
	
	local mx, my = canvas:CursorPos()
	local px, py = mx - offsx, my - offsy
	
	local gw, gh = self:GetWide(), self:GetTall()
	
	--local cx, cy = self._canvas:GetPos()
	//local cx, cy = 0, 0
	local cw, ch = canvas:GetWide(), canvas:GetTall()
	//local cxm, cym = cx + cw, cy + ch
	
	local snap = snapOverride
	local cws, chs = (cw - gw) / snap, (ch - gh) / snap
	local xmar, ymar = px % cws, py % chs
	
	local xgrid = (px - xmar) / cws + (((xmar / cws) < 0.5) and 0 or 1)
	local ygrid = (py - ymar) / chs + (((ymar / chs) < 0.5) and 0 or 1)
	
	return snap, xgrid, ygrid
end

function PANEL:GetVirtualGridPos( )
	return self._iSnapRes, self._iSnapX, self._iSnapY
	
end

function PANEL:ApplyVirtualGridPos( fTime, fEase )
	local xPos = self._iSnapX * ((self._canvas:GetWide() - self:GetWide()) / self._iSnapRes)
	local yPos = self._iSnapY * ((self._canvas:GetTall() - self:GetTall()) / self._iSnapRes)
	//if not fTime or fTime == 0 then
		self:SetPos( xPos, yPos )
	
	//else
	//	self:MoveTo( xPos, yPos, fTime, 0, fEase or 0 )
	//	
	//end
	
end

function PANEL:IsStored( )
	return self._bStored
	
end

function PANEL:SetStore( bStore )
	self._bStored = bStore
	
end

Derma_Hook( PANEL, "PaintOver", "PaintOver", "HPanel" )
Derma_Hook( PANEL, "Paint", "Paint", "HPanel" )
derma.DefineControl( "HPanel", "", PANEL, "DPanel" )
